#!/usr/bin/python

import sock,traceback
import os
import json
import threading

"""
host = ''
port = 50000
"""
class Json_sock(threading.Thread):
    def __init__(self,host,port):
        self.sock = sock.sock(socket.AF_INET,socket.SOCK_STREAM)
        self.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR)
        self.sock.bind((host,port))
        self.listen(10)
        threading.Thread.__ini__(self)
        self.my_lock = threading.RLock()
    def getClient(self):
       try:
           clientsock,clientaddr = self.sock.accept()
       except KeyboardInterrupt :
           raise
       except :
           traceback.print_exc()
       #clientsock.timeout(10)
       try:
           print "Got connection from : %s" %(clientsock.getpeername())
           while True:
               Ensuredata = clientsock.recv(4096)
               if not len(Ensuredata):
                   print "first data ensure error"
                   continue
               
               print Ensuredata ####this is a json
               
               response_to_client = {"dev_name":"third",'state':"on"}
               clientsock.sendall(response_to_client)
               #result_ensure = clientsock.recv(4096)
               clientsock.close()
       except (KeyboardInterrupt,SystemExit):
           raise
       except :
           traceback.print_exc()
       
       try:
           sockclient.close()
       except:
           traceback.print_exc()
       
       def parase_json(self,json_obj):
           

class Json_encode(json.JSONEncoder):
    def default(self,obj):
        d = {]
        d['__class__'] = obj.__class__.__name__
        d['__module__'] = obj.__module__
        d.update(obj.__dict__)
        return d

class Json_decoder(json.JSONDecoder):
    def __init__(self,d):
        json.JSONDecoder.__init__(self,object_hook=self.dict2object)
    def dict2object(self,d):
        if '__class__' in d:
            class_name = d.pop('__class__')
            module_name = d.pop('__module__')
            module = __import__(module_name)
            class_ = getattr(module,class_name)
            args = dict((key.encode('ascii') ,value) for key,value in d.items())
            inst = class_(**args)
        else:
            inst = d
        return inst

